import { computed } from 'vue'
import { useRoute } from 'vue-router'

/**
 * 获取当前路由的 actions 权限码
 * 使用方法：
 * const actions = useRouteActions()
 * v-permission="actions.create"
 */
export function useRouteActions() {
  const route = useRoute()

  const actions = computed(() => {
    // 从当前路由的 meta 中获取 actions
    const meta = route.meta as any
    const routeActions = meta?.actions as Record<string, { title: string; code: string }> | undefined

    if (!routeActions) {
      return {}
    }

    // 将 actions 对象转换为 { create: 'p2z9f5t8', edit: 'r4j6k8m2', ... } 格式
    const result: Record<string, string> = {}
    Object.keys(routeActions).forEach((key) => {
      result[key] = routeActions[key].code
    })

    return result
  })

  return actions
}

